package org.pearl.mybatis.demo.test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.pearl.mybatis.demo.dao.UserMapper;
import org.pearl.mybatis.demo.pojo.entity.User;
import org.pearl.mybatis.demo.pojo.qurey.UserQuery;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by TD on 2021/6/12
 */
public class DynamicTest005 {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 新的
/*        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);*/

        // 动态查询
        UserQuery userQuery = new UserQuery();
        userQuery.setLoginName("zhangwei");
        List<User> dynamicUserList = userMapper.selectDynamicUserList(userQuery);
        System.out.println(dynamicUserList);
        // 再次查询
        //userMapper.updateUserById(new User()); // 更新操作
        sqlSession.clearCache();// 手动清空缓存
        //userQuery.setUserName("李四");
        List<User> dynamicUserList2 = userMapper.selectDynamicUserList(userQuery);
        System.out.println(dynamicUserList2);
        // 判断是否同一对象
        System.out.println("判断是否同一对象" + (dynamicUserList == dynamicUserList2));
        sqlSession.commit();
        sqlSession.close();
    }
}
